package com.dai.uvs.contoller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sun.net.spi.nameservice.NameService;

import com.dai.uvs.data.Equipment;
import com.dai.uvs.data.Land;
import com.dai.uvs.data.Users;
import com.dai.uvs.model.DataManager;
import com.dai.uvs.model.Validation;

/**
 * Servlet implementation class ContollerServlet
 */
public class ContollerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// private Logger logger = Logger.getLogger(this.getClass());
	// private static String uri = "jdbc:db2://localhost:50000/GSDB";
	// public static Connection conn;

	private String dbResult = null;
	private String myException = null;
	private String strError = null;
	private boolean validation = false;
	private int zahl;

	// Connection string data
	private String db_URL;
	private String db_username;
	private String db_password;

	// Instance of Users
	private Users user;
	private Land land;
	private List text = new ArrayList();
	private ArrayList<Equipment> equipment;
	private ArrayList<Equipment> equipments;
	private ArrayList<Land> country;
	private DataManager dataManager;
	private Validation val = new Validation();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ContollerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	/**
	 * initiates the DB2 connection
	 */
	public void init(ServletConfig config) throws ServletException {
		// logger.debug("init()");
		super.init(config);
		dataManager = new DataManager();
		dataManager.setDbURL(config.getInitParameter("dbURL"));
		dataManager.setDbUserName(config.getInitParameter("dbUserName"));
		dataManager.setDbPassword(config.getInitParameter("dbPassword"));

		try {
			Class.forName(config.getInitParameter("jdbcDriver"));
		} catch (Exception ex) {
			System.out.println("Initialize connector string");
			ex.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		if (val.validate(username, password)) {
			Users user = new Users();
			user = dataManager.getUserData(username, password);
			try {
				if (!user.getEmail().equals(null))
					validation = true;
			} catch (NullPointerException np) {
				np.printStackTrace();
				strError = "Invalid username";
				out.println("<center><h3>"+strError+"</h3><center>");
				validation = false;

			}
		} else {
			out.println("<center><h3>Invalid username or password</h3></center>");
			// response.sendRedirect("./Login.html");
			RequestDispatcher rd2 = request
					.getRequestDispatcher("./login.html");
			rd2.include(request, response);
			validation = false;
		}
		
		if(validation){
			HttpSession session = request.getSession(true);
			country = new ArrayList<Land>(dataManager.getComboBoxValues());
			session.setAttribute("country", country);
			equipment = new ArrayList<Equipment>(
					dataManager.getComboBoxValuesEQ());
			session.setAttribute("equipment", equipment);
			session.setAttribute("dataManager", dataManager);
			user = dataManager.getUserLand(username);
			equipments  = dataManager.getGesamt(username);
			session.setAttribute("land", land);
			session.setAttribute("user", user);
			session.setAttribute("equipments", equipments);
			RequestDispatcher rd2 = request
					.getRequestDispatcher("/overview.jsp");
			rd2.forward(request, response);
		}

	}

}
